package org.liziyi.pss.gui;

import org.liziyi.pss.config.Config;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

/**
 * @author liziyi
 */
public class MemoryGUI {
    public JPanel baseJPanel;
    public JTable memoryJTable;

    public void createUIComponents() {
        memoryJTable = new JTable(new MyTableModel(Config.memoryTotalSize, 2));
        memoryJTable.setTableHeader(null);
        DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer(){
            /**
             * 该方法用来渲染表格中每个单元格的样式
             * @param table  the <code>JTable</code>
             * @param value  the value to assign to the cell at
             *                  <code>[row, column]</code>
             * @param isSelected true if cell is selected
             * @param hasFocus true if cell has focus
             * @param row  the row of the cell to render
             * @param column the column of the cell to render
             * @return
             */
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Cell cell=(Cell) value;
                this.setBackground(cell.getColor());
                this.setText(cell.getText());
                return this;
            }
        };
        defaultTableCellRenderer.setHorizontalAlignment(JLabel.CENTER);
        memoryJTable.setDefaultRenderer(Cell.class,defaultTableCellRenderer);
        // 设置列宽度比例
        memoryJTable.getColumnModel().getColumn(1).setPreferredWidth(memoryJTable.getPreferredSize().width*2);
    }

    /**
     * 自定义TableModel,为了能在运行时灵活定制各个单元格的样式（此次主要关注颜色和文本）
     */
    public static class MyTableModel extends AbstractTableModel{
        private Object[][] cells;
        public MyTableModel(int row,int col){
            super();
            cells=new Cell[row][col];
            for(int i=0;i<row;i++){
                for(int j=0;j<col;j++){
                    cells[i][j]=new Cell(""+i,Color.WHITE);
                }
            }
        }
        @Override
        public int getRowCount() {
            return cells.length;
        }

        @Override
        public int getColumnCount() {
            return cells[0].length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return cells[rowIndex][columnIndex];
        }
        @Override
        public String getColumnName(int col) {
            return "";
        }

        @Override
        public Class getColumnClass(int col) {
            return cells[0][col].getClass();
        }
        @Override
        public void setValueAt(Object obj, int row, int col) {
            cells[row][col] = obj;
        }
    }
    public static class Cell{
        private String text;
        private Color color;

        public String getText() {
            return text;
        }

        public Color getColor() {
            return color;
        }

        public Cell(String text, Color color) {
            this.text = text;
            this.color = color;
        }
    }
}
